/**
 * <p>Copyright: Copyright (c) 2013</p>
 */
package com.snail.billing.sso;

import org.apache.http.HttpHost;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

/**
 * 
 * @Title: HttpConnection.java 
 * @Description: 
 * @author Wang Junxiang
 * @date 2014-8-29
 * @version 1.0   
 * @since 1.0
 */
public class HttpConnection implements Connection {
	
	private HttpClient client;
	private int maxConns = 200;
	private int connTimeout = 5000;
	private int routerPerMaxConns = 50;
	private int routerMaxConns = 100;
	private boolean reuse = true;
	private boolean isProxy = false;
	private String proxyIP = "127.0.0.1";
	private int proxyPort = 8087;
	
	private static final class SoufangConnectionHolder {
		private static final HttpConnection instance = new HttpConnection();
	}
	
	private HttpConnection() {
		initClient();
	}
	
	public static HttpConnection getInstance() {
		return SoufangConnectionHolder.instance;
	}
	
	@Override
	public HttpClient getConnection() {
		return client;
	}
	
	protected void initClient() {
		HttpParams params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);  
		HttpProtocolParams.setContentCharset(params, "UTF-8");
		HttpProtocolParams.setUseExpectContinue(params, reuse);
		
		SchemeRegistry schemeRegistry = new SchemeRegistry();  
		schemeRegistry.register(new Scheme("http",  PlainSocketFactory.getSocketFactory(), 80));  
		schemeRegistry.register(new Scheme("https",  SSLSocketFactory.getSocketFactory(), 443));
//		org.apache.http.impl.conn.
//		PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
		ClientConnectionManager connManager = new ThreadSafeClientConnManager(params, schemeRegistry);
//		connManager.setMaxTotal(maxConns);
//		connManager.setDefaultMaxPerRoute(routerPerMaxConns);
//		connManager.setMaxPerRoute(new HttpRoute(new HttpHost("127.0.0.1", 80)), routerMaxConns);
		
		client = new DefaultHttpClient(connManager, params); 
		client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
		
		if(isProxy()) {
			HttpHost proxy = new HttpHost(proxyIP, proxyPort);
			client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
		}
	}
	

	public int getMaxConns() {
		return maxConns;
	}

	public void setMaxConns(int maxConns) {
		this.maxConns = maxConns;
	}

	public int getConnTimeout() {
		return connTimeout;
	}

	public void setConnTimeout(int connTimeout) {
		this.connTimeout = connTimeout;
	}

	public int getRouterPerMaxConns() {
		return routerPerMaxConns;
	}

	public void setRouterPerMaxConns(int routerPerMaxConns) {
		this.routerPerMaxConns = routerPerMaxConns;
	}

	public int getRouterMaxConns() {
		return routerMaxConns;
	}

	public void setRouterMaxConns(int routerMaxConns) {
		this.routerMaxConns = routerMaxConns;
	}

	public boolean isReuse() {
		return reuse;
	}

	public void setReuse(boolean reuse) {
		this.reuse = reuse;
	}

	public boolean isProxy() {
		return isProxy;
	}

	public void setProxy(boolean isProxy) {
		this.isProxy = isProxy;
	}

	public String getProxyIP() {
		return proxyIP;
	}

	public void setProxyIP(String proxyIP) {
		this.proxyIP = proxyIP;
	}

	public int getProxyPort() {
		return proxyPort;
	}

	public void setProxyPort(int proxyPort) {
		this.proxyPort = proxyPort;
	}
}
